<template>
  <div>
    <AplidForm ref="formRef" v-bind="OtherFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
  </div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import { OtherFormConfig } from '../../config'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'

const { setTempParams,setLoading } = useCommonStore()
const { getValue, getContent } = useOperate(2)
const { tempNowObj } = storeToRefs(useCommonStore())
const nowRowData: any = inject('nowRowData')
const emit = defineEmits(['submit'])
const formModel = ref({
  ServiceOrgProduct: [],
  ServiceOrgDevice: [],
})
const formRef = ref(null)
let baseData = markRaw({})
let baseProduct = markRaw({})
let baseDevice = markRaw({})

const onSubmit = (form, done) => {
  const params = {
    ...form,
    // 加上组织名称，否则报错
    organizationName:nowRowData.organizationName,
    creditCode: tempNowObj.value.creditCode,
    code: 1003,
  }

  // 处理变更字段
  let changeColumnsBase = OtherFormConfig.column.filter((item) => item.changeField)
  let changeData = []
  changeColumnsBase.map((item) => {
    const tempBefore = baseData[item.propName || item.prop]
    const tempAfter = params[item.propName || item.prop]
    if (tempBefore != tempAfter) {
      changeData.push({
        label: item.label,
        before: getValue(tempBefore, item),
        after: getValue(tempAfter, item),
      })
    }
  })

  const emptyObj = {
    organizationName: tempNowObj.value.organizationName,
    creditCode: tempNowObj.value.creditCode,
    code: undefined,
    ServiceOrgProduct: undefined,
    ServiceOrgDevice: undefined,
  }

  const changeObj = {
    data: changeData,
    dataChangeContentList: [
      ...getContent(baseProduct, params.ServiceOrgProduct, 3018, '养老机构-老年用品', emptyObj, 'id'),
      ...getContent(baseDevice, params.ServiceOrgDevice, 3014, '养老机构-设施设备', emptyObj, 'id'),
      {
        changeBefore: {
          ...baseData,
          ...emptyObj,
          // 加上基础信息的机构名称和统一信用代码，否则报错
          organizationName:nowRowData.organizationName,
          creditCode: nowRowData.creditCode,
        },
        changeAfter: {
          ...params,
          ...emptyObj,
          // 加上基础信息的机构名称和统一信用代码，否则报错
          organizationName:nowRowData.organizationName,
          creditCode: nowRowData.creditCode,
        },
        changeData: {
          ...params,
          ...emptyObj,
          // 加上基础信息的机构名称和统一信用代码，否则报错
          organizationName:nowRowData.organizationName,
          creditCode: nowRowData.creditCode,
        },
        changeType: '养老机构-其他信息',
        code: 3017,
      },
    ],
  }
  setTempParams({
    name: params.organizationName,
    organizationName: params.organizationName,
    creditCode: params.creditCode,
    nowChange: changeObj,
  })
  console.log('信息比较',changeObj)
  emit('submit', changeObj)
  done()
}

const getInitData = () => {
  const query = { creditCode: nowRowData.creditCode }
  setLoading()
  Promise.all([apiMod.view_dpServiceOrgOther_page(query), apiMod.view_dpServiceOrgProduct_page(query), apiMod.view_dpServiceOrgDevice_page(query)])
    .then((res) => {
      return res.map((item) => item.data.records)
    })
    .then((res) => {
      setLoading()
      const otherData = res[0][0] || {}
      formModel.value = otherData
      formModel.value.ServiceOrgProduct = res[1]
      formModel.value.ServiceOrgDevice = res[2]

      baseData = Object.freeze(JSON.parse(JSON.stringify(otherData)))
      baseProduct = Object.freeze(JSON.parse(JSON.stringify(res[1])))
      baseDevice = Object.freeze(JSON.parse(JSON.stringify(res[2])))
    })
}

onMounted(() => {
  getInitData()
})

const onSave = () => {
  formRef.value!.onSubmit()
}
defineExpose({
  onSave,
})
</script>

<style scoped></style>
